Skip to content

Comments

fix(HitsList): remove race condition#346

Merged
Haroenv merged 2 commits intomasterfrom
fix/race-condition
Jan 7, 2026
Merged

fix(HitsList): remove race condition#346
Haroenv merged 2 commits intomasterfrom
fix/race-condition

Conversation

@Haroenv
Copy link
Contributor

@Haroenv Haroenv commented Jan 5, 2026

Problem

The HitsList SwiftUI view crashes with an index out of bounds error when search results update during rendering.

Root Cause: Race condition between ForEach iteration and the @Published hits array being updated. ForEach captures the array count, but before rendering completes, reload() updates the array (potentially from a background thread). When SwiftUI accesses hits[index], the array may have shrunk, causing a crash.

Solution

  1. Thread Safety: Wrap reload() updates in DispatchQueue.main.async to ensure @Published changes happen on main thread
  2. Bounds Checking: Check array bounds before access, passing nil to the row closure if index is out of bounds

### Problem

The `HitsList` SwiftUI view crashes with an index out of bounds error when search results update during rendering.

**Root Cause:** Race condition between `ForEach` iteration and the `@Published` hits array being updated. `ForEach` captures the array count, but before rendering completes, `reload()` updates the array (potentially from a background thread). When SwiftUI accesses `hits[index]`, the array may have shrunk, causing a crash.

### Solution

1. **Thread Safety**: Wrap `reload()` updates in `DispatchQueue.main.async` to ensure `@Published` changes happen on main thread
2. **Bounds Checking**: Check array bounds before access, passing `nil` to the row closure if index is out of bounds
@Haroenv Haroenv merged commit 0bd0d9b into master Jan 7, 2026
11 of 13 checks passed
@Haroenv Haroenv deleted the fix/race-condition branch January 7, 2026 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants